﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace Opo.Perspective.Core
{
    /// <summary>
    /// Represents a service to access data of type IPerspectiveEntity
    /// </summary>
    /// <typeparam name="T">Type of the accessing data</typeparam>
    public class DataService<T> : IService<T> where T : IPerspectiveEntity
    {
        private IRepository<T> _repository;

        /// <summary>
        /// Initializes a new instance of the <see cref="DataService&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="repository">The repository.</param>
        public DataService(IRepository<T> repository)
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository");
            }
            _repository = repository;
        }

        /// <summary>
        /// Adds the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public T Add(T entity)
        {
            return _repository.Add(entity);
        }

        /// <summary>
        /// Gets the entity specified by the id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public T Get(Guid id)
        {
            return _repository.Get(id);
        }

        /// <summary>
        /// Gets all entities.
        /// </summary>
        /// <returns></returns>
        public IList<T> GetAll()
        {
            return _repository.GetAll().ToList();
        }

        /// <summary>
        /// Updates the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public T Update(T entity)
        {
            return _repository.Update(entity);
        }

        /// <summary>
        /// Deletes the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public void Delete(T entity)
        {
            _repository.Delete(entity);
        }
    }
}
